/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.server.model;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import offset.nodes.server.config.NodesConfig;
import offset.nodes.server.version.model.CheckinStartupExtension;
import offset.nodes.server.workflow.controller.startup.WorkflowDocumentStartupExtension;

/**
 *
 * @author Walter Lütgenau
 */
public class RepositoryExtensionsManager {

    static boolean started = false;

    /**
     * Start the extensions, if this has not been done before.
     * 
     * @param systemSession Pass the system session to the extensions
     */
    public static synchronized void startUp(Session systemSession) {
        try {
            if (started)
                return;
            
            RepositoryExtensionsManager instance = new RepositoryExtensionsManager();
            instance.startExtensions(systemSession);

            started = true;
        } catch (RepositoryException ex) {
            Logger.getLogger(RepositoryExtensionsManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Start static extensions
     * 
     * @param session
     */
    public void startExtensions(Session session) throws RepositoryException {
        for (RepositoryStartupExtension extension : NodesConfig.getInstance().getStartupExtensionConfig().getStartupExtensions())
            extension.onStartup(session);
    }

}
